package com.sun.weekend.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sun.weekend.R;
import com.sun.weekend.beans.TaskBean;
import com.sun.weekend.view.CustomDialog;
import com.sun.weekend.view.FixedGridView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * @author sunleilei
 */

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder>{

    private Context mContext;
    LayoutInflater mInflater;
    List<TaskBean> items = new ArrayList<>();
    CustomDialog commentDialog;
    public TaskAdapter(Context mContext){
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void append(List list) {
        items.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }
    @Override
    public TaskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.task_item,null);
        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final TaskViewHolder holder, int position) {
        final TaskBean bean = items.get(position);
        holder.user_name.setText(bean.getAuthor().getUsername());
        holder.title.setText(bean.getTitle());
        holder.extra_info.setText(bean.getContent());
        holder.favor.setText(bean.getFavors() != null ?bean.getFavors()+1+"":"0");
//        Glide.with(mContext).load(bean.getAuthor().getAvstar() != null?bean.getAuthor().getAvstar().getFileUrl():"")
//                .placeholder(R.mipmap.ic_launcher).into(holder.user_icon);
        if (bean.getContentFile() != null) {
            holder.gridView.setAdapter(new ImageGridAdapter(mContext).append(bean.getContentFile()));
        }

        holder.favor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.increment("favors");
                bean.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        holder.favor.setText(bean.getFavors() != null ?bean.getFavors()+1+"":"0");
                    }
                });

            }
        });

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public class TaskViewHolder extends RecyclerView.ViewHolder{

        private FixedGridView gridView;
        private ImageView user_icon;
        private TextView title;
        private TextView user_name;
        private TextView extra_info;
        private TextView favor;
        public TaskViewHolder(View itemView) {
            super(itemView);
            gridView = (FixedGridView) itemView.findViewById(R.id.image_grid);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            extra_info = (TextView) itemView.findViewById(R.id.task_extra_info);
            favor = (TextView) itemView.findViewById(R.id.favor);
            title = (TextView) itemView.findViewById(R.id.title);
            user_icon = (ImageView) itemView.findViewById(R.id.user_icon);
        }
     }
}
